//
//  UIBarButtonItem+BarButtonClick.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/20.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

extension UIBarButtonItem
{
    //扩展新功能
    //在扩展中使用便利构造函数
    convenience init(title:String?,imageStr:String?,target: AnyObject?, action: Selector)
    {
        self.init()
        
        let button:UIButton = UIButton()
        
        //如果有title
        if let wordtitle = title
        {
            button.setTitle(wordtitle, forState: UIControlState.Normal)
            
            button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
            button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)
            
            
            button.titleLabel?.font = UIFont.systemFontOfSize(15)
        }
        
        //如果有imageStr
        if let imageString = imageStr
        {
            button.setImage(UIImage(named: imageString), forState: UIControlState.Normal)
        }
        
        //设置点击事件
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置button自适应
        button.sizeToFit()
        
        //设置button为CustomView
        self.customView = button
        
    }
    
}
